package net.ib.mtalk.ui.fragment;

import net.ib.mtalk.R;
import net.ib.mtalk.database.DatabaseContract.MessageTable;
import net.ib.mtalk.database.DatabaseContract.ViewTable;
import net.ib.mtalk.ui.chat.ChatListActivity;
import net.ib.mtalk.ui.chat.ChatListCursorAdapter;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Chat ListView
 * 
 * @author happy2v
 *
 */
public class ChatListFragment extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {

	static CursorAdapter mAdapter;

	String mCurFilter;


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setHasOptionsMenu(true);
		ListView lv = getListView();
		lv.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
		lv.setCacheColorHint(Color.TRANSPARENT);
		lv.setSelector(R.drawable.selector_transparent);
		lv.setDivider(null);

		mAdapter = new ChatListCursorAdapter(getActivity(),	null, 0);
		setListAdapter(mAdapter);
		

		
		getLoaderManager().initLoader(0, savedInstanceState, this);
	}

	@Override
	public void onResume() {
		
		super.onResume();
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
	}

	public boolean onQueryTextChange(String newText) {
		mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
		getLoaderManager().restartLoader(0, null, this);
		return true;
	}

	// row 클릭시 이벤트
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Log.i("FragmentComplexList", "Item clicked: " + id
				+ ", position : " + position);
	}

	// 커서를 설정하고 데이터를 불러오는 부분
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		Uri chatListUri = MessageTable.CONTENT_URI;
		
		String select = "("+ViewTable.KEY_CLIENT_VIEW_UUID_MSB+"=? AND "+ ViewTable.KEY_CLIENT_VIEW_UUID_LSB +"=? )";
		String sortOrder = MessageTable.KEY_MESSAGE_TIME + " COLLATE LOCALIZED ASC"; // MessageTable.KEY_MESSAGE_UUID_MSB +", " + MessageTable.KEY_MESSAGE_UUID_LSB + " ASC";
		return new CursorLoader(getActivity(), chatListUri,
				MessageTable.PROJECTION, select, 
				new String[]{String.valueOf(((ChatListActivity)getActivity()).mClientViewUuidMsb), String.valueOf(((ChatListActivity)getActivity()).mClientViewUuidLsb)},
				sortOrder);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		mAdapter.swapCursor(data);
		setLastPosition();
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		mAdapter.swapCursor(null);
	}
	
	@Override
	public ListAdapter getListAdapter() {
		return super.getListAdapter();
	}
	
	public void setLastPosition(){
		getListView().setSelection(getListAdapter().getCount()-1);
	}
}